﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipping.Utils;
using System.IO;
using BLToolkit.Data.Linq;
using Clipping.Infrastructure.Models;
namespace Clipping.Utils
{
    public class SettingRepository
    {

        static DBConnectionPool sysSettingsDBConnectionPool;
        static DBConnectionPool userSettingsDBConnectionPool;
        static SettingRepository()
        {
            string sysSettingsDBPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB", "SysSettings.db");
            sysSettingsDBConnectionPool = DBHelper.CreateDBConnectionPool<SysSettingsDBUpdater>(sysSettingsDBPath, 10);

            string userSettingsDBPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB", "UserSettings.db");
            userSettingsDBConnectionPool = DBHelper.CreateDBConnectionPool<SysSettingsDBUpdater>(userSettingsDBPath, 10);

        }

        public SysSettings GetSysSettings()
        {
            SysSettings rtn = new SysSettings();
            using (DBConnection conn = sysSettingsDBConnectionPool.GetConnection())
            {
                var table = SYSSETTINGS.Records;
                if (table != null)
                {
                    rtn.UploadUrl = tryGetValue(table, "UploadUrl");
                    rtn.UploadUserName = tryGetValue(table, "UploadUserName");
                    rtn.UploadPassword = tryGetValue(table, "UploadPassword");
                    rtn.ClientInfoUrl = tryGetValue(table, "ClientInfoUrl");
                    rtn.MediaInfoUrl = tryGetValue(table, "MediaInfoUrl");
                    rtn.UserInfoUrl = tryGetValue(table, "UserInfoUrl");
                    rtn.AutoUpdate = tryGetBoolValue(table, "AutoUpdate");
                    rtn.Encoding = tryGetValue(table, "Encoding");
                    rtn.EmbedPic = tryGetBoolValue(table, "EmbedPic");
                    rtn.ZipClipping = tryGetBoolValue(table, "ZipClipping");
                }
                conn.Free();
            }
            return rtn;
        }

        private bool tryGetBoolValue(Table<SYSSETTINGS> table, string key)
        {
            string value = tryGetValue(table, key);
            if (value == "True")
                return true;
            else
                return false;
        }

        private string tryGetValue(Table<SYSSETTINGS> table, string key)
        {
            var record = table.FirstOrDefault(k => k.Key == key);
            if (record != null)
                return record.Value;
            else
                return null;
        }

        public void SaveSysSettings(SysSettings settings)
        {
            if (string.IsNullOrEmpty(settings.Encoding))
                settings.Encoding = "UTF-8";
            using (DBConnection conn = sysSettingsDBConnectionPool.GetConnection())
            {
                var records = SYSSETTINGS.Records;
                saveSysSettingValue(records, "UploadUrl", settings.UploadUrl);
                saveSysSettingValue(records, "UploadUserName", settings.UploadUserName);
                saveSysSettingValue(records, "UploadPassword", settings.UploadPassword);
                saveSysSettingValue(records, "ClientInfoUrl", settings.ClientInfoUrl);
                saveSysSettingValue(records, "MediaInfoUrl", settings.MediaInfoUrl);
                saveSysSettingValue(records, "UserInfoUrl", settings.UserInfoUrl);
                saveSysSettingValue(records, "AutoUpdate", settings.AutoUpdate.ToString());
                saveSysSettingValue(records, "Encoding", settings.Encoding);
                saveSysSettingValue(records, "EmbedPic", settings.EmbedPic.ToString());
                saveSysSettingValue(records, "ZipClipping", settings.ZipClipping.ToString());
                conn.Free();
            }
        }


        private bool saveSysSettingValue(Table<SYSSETTINGS> table, string key, string value)
        {
            if (string.IsNullOrEmpty(value))
                value = "";
            return table.InsertOrUpdate(
                 () => new SYSSETTINGS() { Key = key, Value = value },
                k => new SYSSETTINGS() { Value = value },
                () => new SYSSETTINGS() { Key = key }) != 0;
        }




        public UserSettings GetUserSettings()
        {
            UserSettings rtn = new UserSettings();
            using (DBConnection conn = userSettingsDBConnectionPool.GetConnection())
            {
                var table = USERSETTINGS.Records;
                if (table != null)
                {
                    rtn.AutoGetContent = tryGetBoolValue(table, "AutoGetContent");
                    rtn.AutoOpenNews = tryGetBoolValue(table, "AutoOpenNews");
                    rtn.AutoCountWords = tryGetBoolValue(table, "AutoCountWords");
                    rtn.UserName = tryGetValue(table, "UserName");
                    rtn.SelectedSearchEngines = tryGetValue(table, "SelectedSearchEngines");
                    rtn.DefaultAD = tryGetValue(table, "DefaultAD");
                    rtn.DefaultHeight = tryGetValue(table, "DefaultHeight");
                    rtn.DefaultWidth = tryGetValue(table, "DefaultWidth");
                    rtn.DefaultPage = tryGetValue(table, "DefaultPage");
                    rtn.DefaultPageSize = tryGetValue(table, "DefaultPageSize");
                    rtn.DefaultWords = tryGetValue(table, "DefaultWords");
                    rtn.WindowState = tryGetValue(table, "WindowState");
                }
                conn.Free();
            }
            return rtn;
        }

        private bool tryGetBoolValue(Table<USERSETTINGS> table, string key)
        {
            string value = tryGetValue(table, key);
            if (value == "True")
                return true;
            else
                return false;
        }

        private string tryGetValue(Table<USERSETTINGS> table, string key)
        {
            var record = table.FirstOrDefault(k => k.Key == key);
            if (record != null)
                return record.Value;
            else
                return null;
        }

        public void SaveUserSettings(UserSettings settings)
        {
            using (DBConnection conn = userSettingsDBConnectionPool.GetConnection())
            {
                var records = USERSETTINGS.Records;
                saveUserSettingValue(records, "AutoGetContent", settings.AutoGetContent.ToString());
                saveUserSettingValue(records, "AutoOpenNews", settings.AutoOpenNews.ToString());
                saveUserSettingValue(records, "AutoCountWords", settings.AutoCountWords.ToString());
                saveUserSettingValue(records, "UserName", settings.UserName);
                saveUserSettingValue(records, "SelectedSearchEngines", settings.SelectedSearchEngines);
                saveUserSettingValue(records, "DefaultAD", settings.DefaultAD);
                saveUserSettingValue(records, "DefaultHeight", settings.DefaultHeight);
                saveUserSettingValue(records, "DefaultWidth", settings.DefaultWidth);
                saveUserSettingValue(records, "DefaultPage", settings.DefaultPage);
                saveUserSettingValue(records, "DefaultPageSize", settings.DefaultPageSize);
                saveUserSettingValue(records, "DefaultWords", settings.DefaultWords);
                saveUserSettingValue(records, "WindowState", settings.WindowState);
                conn.Free();
            }
        }

        private bool saveUserSettingValue(Table<USERSETTINGS> table, string key, string value)
        {
            if (string.IsNullOrEmpty(value))
                value = "";
            return table.InsertOrUpdate(
                 () => new USERSETTINGS() { Key = key, Value = value },
                k => new USERSETTINGS() { Value = value },
                () => new USERSETTINGS() { Key = key }) != 0;
        }
    }
}
